package com.youdoneed.common.controller;

import com.youdoneed.base.application.ResultMessage;
import com.youdoneed.common.model.VersionInfo;
import com.youdoneed.common.service.VersionService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@RequestMapping("/version")
@Api(value = "版本控制器", description = "版本控制器")
public class VersionController {

    @Autowired
    private VersionService versionService;

    /**
     * 获取最新版本的版本号
     */
    @ApiOperation("获取最新版本的版本号")
    @RequestMapping(value = "/getNewestVersionInfo", method = {RequestMethod.GET})
    public ResultMessage<VersionInfo> getNewestVersionInfo() {
        VersionInfo versionInfo = versionService.getNewestVersionInfo();
        if (versionInfo != null) {
            return new ResultMessage<VersionInfo>(versionInfo).toSuccess();
        } else {
            return new ResultMessage<VersionInfo>(versionInfo).toFailure("获取最新版本信息失败");
        }


    }

    /**
     * 上传最新版本apk
     */
    @ApiOperation("上传最新版本apk")
    @RequestMapping(value = "/upLoadNewApk", method = {RequestMethod.POST})
    public ResultMessage<String> upLoadNewApk(@ApiParam("版本号") @RequestParam int versionCode,
                                                   @ApiParam("版本名称") @RequestParam String versionName,
                                                   @ApiParam("描述") @RequestParam String description,
                                                   @RequestPart("apkFile") MultipartFile apkFile) {
        if (versionService.upLoadNewApk(versionCode, versionName, description, apkFile)>0){
            return new ResultMessage<>("保存成功").toSuccess();
        }else {
            return new ResultMessage<>("保存失败").toFailure();
        }
    }

}
